//options
clear all
set maxvar  32000, permanently
set matsize 11000, permanently
set more off, permanently
global bootstraps 1000
set seed 23

// macros
global klmshare				: env klmshare
global projects				: env projects
global klmperry             : env klmperry
global storageb				: env storageb

global perrydatas       = "${klmperry}/CBA/data/perry/raw"
global perrydata        = "${klmperry}/CBA/data/perry/clean"
global masterinter  	= "${klmshare}/CurrentRAs/FredB/test"
global output           = "${projects}/ece_parenting/tex_files/other"
global dataperry        = "$klmperry/PerryPreschool/Data/Perry_PARI_and_Other_Data/PARI/DATA_PARI/"
global abcjpeanalysis   = "${klmshare}/Data_Central/Abecedarian/data/ABC-CARE/extensions/cba-iv/"
global dataihdp         = "${projects}/ece_parenting/data"
global dataihdp         = "$storageb/dc_data/"

cd  $dataihdp
use ihdp_data, clear
rename _all, lower

replace   bw = bw/1000
destring bwg , replace 


// impute iq
replace   stndscor =  iqcage     if stndscor ==.
replace   iqcage   =  stndscor   if iqcage ==.
foreach var of varlist stndscor iqcage { 
	replace `var' = mdi_24cor    if stndscor ==. & iqcage ==.
}
replace   stndscor =  iqcage     if stndscor ==.
replace   iqcage   =  stndscor   if iqcage ==.


// list relevant variables
global baseline_child      twin sex bw anga black hispanic 
global baseline_mother     mage meduc works married 
global baseline_household  welfare tot_siblings_natural employed_adult
global baseline_economy    employment medinc gpc
global outputs             iqcage stndscor 
global inputs              cum_avg_daycare_36m_sum

// mark all sample
reg $baseline_child
gen sample0 = e(sample)

// mark baseline sample
reg $baseline_child $baseline_mother $baseline_household $baseline_economy $outputs $inputs
gen sample1 = e(sample)

// parenting latents, ages 1 and 3
foreach var of varlist alt_subscale_1_12-alt_subscale_6_12 alt_subscale_1_36-alt_subscale_8_36 {
	summ `var'                               if sample1 == 1
	gen  `var'_std = (`var' - r(mean))/r(sd) if sample1 == 1
}
# delimit
sem (_cons@0 X -> alt_subscale_1_12_std) (_cons@0 X -> alt_subscale_2_12_std) (_cons@0 X -> alt_subscale_3_12_std) 
	(_cons@0 X -> alt_subscale_4_12_std) (_cons@0 X -> alt_subscale_5_12_std) (_cons@0 X -> alt_subscale_6_12_std) if sample1 == 1, method(adf);
predict parenting_age1 if sample1 == 1, latent;
sem (_cons@0 X -> alt_subscale_1_36_std) (_cons@0 X -> alt_subscale_2_36_std) (_cons@0 X -> alt_subscale_3_36_std) (_cons@0 X -> alt_subscale_4_36_std)
	(_cons@0 X -> alt_subscale_5_36_std) (_cons@0 X -> alt_subscale_6_36_std) (_cons@0 X -> alt_subscale_7_36_std) (_cons@0 X -> alt_subscale_8_36_std) if sample1 == 1, 
																																						 cov(e.alt_subscale_1_36_std*e.alt_subscale_2_36_std)
																																						 cov(e.alt_subscale_3_36_std*e.alt_subscale_4_36_std)
																																						 cov(e.alt_subscale_5_36_std*e.alt_subscale_6_36_std)
																																						 cov(e.alt_subscale_7_36_std*e.alt_subscale_8_36_std)
																																						 cov(e.alt_subscale_4_36_std*e.alt_subscale_7_36_std)
																																						 cov(e.alt_subscale_2_36_std*e.alt_subscale_7_36_std) method(adf);		
 predict parenting_age3 if sample1 == 1, latent;
# delimit cr
egen    parenting_ages13 = rowmean(parenting_age1 parenting_age3) if sample1 == 1

// standardize latents in sample
foreach var of varlist parenting_ages13 {
	summ    `var'                               if tg == 0 & sample1 == 1
	gen     `var'_std = (`var' - r(mean))/r(sd) if sample1 == 1
	replace `var'_std = `var'_std + 100         if sample1 == 1
}
// log and standardize childcare variable 
replace cum_avg_daycare_36m_sum =.                                              if sample1 != 1
summ    cum_avg_daycare_36m_sum                                                 if tg == 0 & sample1 == 1
replace cum_avg_daycare_36m_sum  = (cum_avg_daycare_36m_sum    - r(mean))/r(sd) if sample1 == 1
replace cum_avg_daycare_36m_sum  =  cum_avg_daycare_36m_sum    + r(mean)        if sample1 == 1

global inputs_std   cum_avg_daycare_36m_sum parenting_ages13_std
keep   ihdp site tg bwg sample* $baseline_child $baseline_mother $baseline_household $baseline_economy $inputs_std $outputs ppvtstd wasifsiq

rename cum_avg_daycare_36m_sum c
rename parenting_ages13_std    par

// merge in ages 5 and 8
merge 1:1 ihdp using iqchildhoodihdp
keep  if _merge == 3
drop _merge

// impute iq
replace kidppvt5 = wippsif5 if kidppvt5 ==.
replace wippsif5 = kidppvt5 if wippsif5 ==.

replace ppvtstd8 = fsiq8    if ppvtstd8 ==.
replace fsiq8    = ppvtstd8 if fsiq8    ==.

replace ppvtstd  = wasifsiq if ppvtstd  ==.
replace wasifsiq = ppvtstd  if wasifsiq ==.

reg fsiq8 ppvtstd8 kidppvt5 wippsif5 wasifsiq ppvtstd
gen sample2 = e(sample) 

// outcomes
merge 1:1 ihdp using adultihdp
keep if _merge == 3
drop    _merge

// construct "positive" outcomes
replace idle18y         = 1 - idle18y
replace sch_eversped18y = 1 - sch_eversped18y
replace sch_math18y     = 1 - sch_math18y
replace sch_read18y     = 1 - sch_read18y
replace sch_thrp18y     = 1 - sch_thrp18y
replace teen18y         = 1 - teen18y

gen     smoke18 = .
replace smoke18 = 1 if cigs18y >  0  & cigs18y != .
replace smoke18 = 0 if cigs18y == 0 & cigs18y != .
replace smoke18 = 1 - smoke18

gen     absence18 = .
replace absence18 = 0 if sch_dabs18y < 5  & sch_dabs18y != .
replace absence18 = 1 if sch_dabs18y >= 5 & sch_dabs18y != .
replace absence18 = 1 - absence18 

// marking missing
global ed18  sch_eversped18y sch_math18y sch_read18y sch_test18y
global beh18 smoke18 idle18y sch_thrp18y teen18y
egen missing     = rowmiss($ed18 $beh18)

// average
egen avg_outcome_educ18 = rowmean($ed18)        if missing == 0
egen avg_outcome_beh18  = rowmean($beh18)       if missing == 0
egen avg_outcome18      = rowmean(avg_outcome_educ18 avg_outcome_beh18) if missing == 0

// define sample
reg     avg_outcome18 if sample2 == 1
replace sample2 = 0   if e(sample) == 0
replace sample2 = 0   if   sample1 == 0

// lists of outcomes
global inputs c par 
global iq3    iqcage stndscor
global iq5    kidppvt5 wippsif5
global iq8    ppvtstd8 fsiq8
global iq18   iq18y
global niq18  avg_outcome18

// describe by sample
global sample_all          if twin != .
global sample_singletons   if twin == 0
global sample_twins        if twin == 1

// controls
global baseline_child      twin sex bw anga black hispanic 
global baseline_mother     mage meduc works married 
global baseline_household  welfare tot_siblings_natural employed_adult
global baseline_economy    employment medinc gpc
tab site, gen(site_)
global controls $baseline_child $baseline_mother $baseline_household $baseline_economy site_1-site_7

// weights
reg  sample2 $controls                   if sample1 == 1
predict ipw                              if sample1 == 1, xb
summ    ipw                              if sample1 == 1
replace ipw = ipw + abs(r(min)) + .001   if sample1 == 1

// list 
global csample             sample1 == 1
global parsample           sample1 == 1
global iqcagesample        sample1 == 1
global stndscorsample      sample1 == 1
global avg_outcome18sample sample2 == 1

matrix all = J(1,10,.)
// roll md
foreach var of varlist $inputs $iq3 $niq18 {
	foreach group in singletons twins {
		// spec 1
		bootstrap, strata(bwg site tg) reps($bootstraps) :         reg `var' tg ${sample_`group'} & ${`var'sample}
		matrix  b = e(b)[1,1]
		matrix se      = sqrt(e(V)[1,1])
		matrix  t      = abs(b[1,1]/se[1,1])
		matrix  p      = 2*(1 - normal(t[1,1]))
		matrix b`var'_`group'_spec1 = [b]
		matrix p`var'_`group'_spec1 = [p]
		
		// spec 2
		bootstrap, strata(bwg tg) reps($bootstraps) cluster(site): reg `var' tg ${sample_`group'} & ${`var'sample}
		matrix  b = e(b)[1,1]
		matrix se      = sqrt(e(V)[1,1])
		matrix  t      = abs(b[1,1]/se[1,1])
		matrix  p      = 2*(1 - normal(t[1,1]))
		matrix p`var'_`group'_spec2 = [p]
		
		// spec 3
		reg `var' tg ${sample_`group'} & sample1 == 1 & ${`var'sample}, robust
		matrix se      = sqrt(e(V)[1,1])
		matrix  t      = abs(b[1,1]/se[1,1])
		matrix  p      = 2*(1 - normal(t[1,1]))
		matrix p`var'_`group'_spec3 = [p]
		
		// spec 4
		reg `var' tg ${sample_`group'} & sample1 == 1 & ${`var'sample}, robust cluster(site)
		matrix se      = sqrt(e(V)[1,1])
		matrix  t      = abs(b[1,1]/se[1,1])
		matrix  p      = 2*(1 - normal(t[1,1]))
		matrix p`var'_`group'_spec4 = [p]
		
		matrix `var'_`group' = [b,p`var'_`group'_spec1,p`var'_`group'_spec2,p`var'_`group'_spec3,p`var'_`group'_spec4]
	}
	matrix `var' = [`var'_singletons,`var'_twins]
	matrix all = [all \ `var']
}

// roll ols
foreach var of varlist $inputs $iq3 $niq18 {
	foreach group in singletons twins {
		// spec 1
		bootstrap, strata(bwg site tg) reps($bootstraps) :         reg `var' tg $controls ${sample_`group'} & ${`var'sample}
		matrix  b = e(b)[1,1]
		matrix se      = sqrt(e(V)[1,1])
		matrix  t      = abs(b[1,1]/se[1,1])
		matrix  p      = 2*(1 - normal(t[1,1]))
		matrix b`var'_`group'_spec1 = [b]
		matrix p`var'_`group'_spec1 = [p]
		
		// spec 2
		bootstrap, strata(bwg tg) reps($bootstraps) cluster(site): reg `var' tg $controls  ${sample_`group'} & ${`var'sample}
		matrix se      = sqrt(e(V)[1,1])
		matrix  t      = abs(b[1,1]/se[1,1])
		matrix  p      = 2*(1 - normal(t[1,1]))
		matrix p`var'_`group'_spec2 = [p]
		
		// spec 3
		reg `var' tg $controls ${sample_`group'} & sample1 == 1 & ${`var'sample}, robust
		matrix se      = sqrt(e(V)[1,1])
		matrix  t      = abs(b[1,1]/se[1,1])
		matrix  p      = 2*(1 - normal(t[1,1]))
		matrix p`var'_`group'_spec3 = [p]
		
		// spec 4
		reg `var' tg $controls ${sample_`group'} & sample1 == 1 & ${`var'sample}, robust cluster(site)
		matrix se      = sqrt(e(V)[1,1])
		matrix  t      = abs(b[1,1]/se[1,1])
		matrix  p      = 2*(1 - normal(t[1,1]))
		matrix p`var'_`group'_spec4 = [p]
		
		matrix `var'_`group' = [b,p`var'_`group'_spec1,p`var'_`group'_spec2,p`var'_`group'_spec3,p`var'_`group'_spec4]
	}
	matrix `var' = [`var'_singletons,`var'_twins]
	matrix all = [all \ `var']
}

// roll ipw
gen sitenum = .
foreach num of numlist 1(1)8 {
	replace sitenum = `num' if site_`num' == 1
}
gen weights = 1
foreach var of varlist $inputs $iq3 $niq18 {
	foreach group in singletons twins {
		// spec 1
		reg `var' tg $controls ${sample_`group'} & ${`var'sample} [aw = ipw]
		matrix  b = e(b)[1,1]
		
		matrix Bspec1 = [.]
		preserve
		foreach b of numlist 1(1)$bootstraps {
			bsample, strata(bwg site tg)
			reg `var' tg $controls ${sample_`group'} & ${`var'sample} [aw = ipw]
			matrix  b`b' = e(b)[1,1]
			matrix Bspec1 = [Bspec1 \ b`b']
		}
		restore
		
		// spec 2		
		matrix Bspec2 = [.]
		preserve
		foreach b of numlist 1(1)$bootstraps {
			capture bsample, strata(bwg tg) cluster(sitenum)
			reg `var' tg $controls ${sample_`group'} & ${`var'sample} [aw = ipw]
			matrix  b`b' = e(b)[1,1]
			matrix Bspec2 = [Bspec2 \ b`b']
		}
		restore
		
		foreach spec in 1 2 { 
			preserve
			clear
			matrix Bspec`spec' = Bspec`spec'[2...,1]
			matrix colnames Bspec`spec' = Bspec`spec'
			svmat           Bspec`spec', names(col)
			duplicates drop Bspec`spec', force
			summ Bspec`spec'
			matrix se = r(sd)
			matrix  t      = abs(b[1,1]/se[1,1])
			matrix  p      = 2*(1 - normal(t[1,1]))
			matrix p`var'_`group'_spec3 = [p]
			restore
		}
		
		// spec 3
		reg `var' tg $controls ${sample_`group'} & sample1 == 1 & ${`var'sample} [aw = ipw], robust
		matrix  b = e(b)[1,1]
		matrix se      = sqrt(e(V)[1,1])
		matrix  t      = abs(b[1,1]/se[1,1])
		matrix  p      = 2*(1 - normal(t[1,1]))
		matrix p`var'_`group'_spec3 = [p]
		
		// spec 4
		reg `var' tg $controls ${sample_`group'} & sample1 == 1 & ${`var'sample} [aw = ipw], robust cluster(site)
		matrix  b = e(b)[1,1]
		matrix se      = sqrt(e(V)[1,1])
		matrix  t      = abs(b[1,1]/se[1,1])
		matrix  p      = 2*(1 - normal(t[1,1]))
		matrix p`var'_`group'_spec4 = [p]
		
		matrix `var'_`group' = [b,p`var'_`group'_spec1,p`var'_`group'_spec2,p`var'_`group'_spec3,p`var'_`group'_spec4]
	}
	matrix `var' = [`var'_singletons,`var'_twins]
	matrix all = [all \ `var']
}
matrix all = all[2...,1...]


matrix all = [ J(7,10,.)  \ all[1..5,1...] \ J(1,10,.) \ all[6..10,1...] \ J(1,10,.) \ all[11..15,1...] ]
clear
svmat all
gen   n = _n

// format
foreach var of varlist all* {
	gen    `var'and = "&" if n !=1
}

// header
gen all0and = "&" if n != 1
gen       all1_0 = "\begin{tabular}{ l cccccccccc}  \toprule" if n == 1

replace all1_0   = " "                    	      		      if n == 2
replace all0and  = " & \multicolumn{1}{c}{(1)}"     if n == 2
replace all1and  = " & \multicolumn{1}{c}{(2)}"     if n == 2
replace all2and  = " & \multicolumn{1}{c}{(3)}"     if n == 2
replace all3and  = " & \multicolumn{1}{c}{(4)}"     if n == 2
replace all4and  = " & \multicolumn{1}{c}{(5)}"     if n == 2
replace all5and  = " & \multicolumn{1}{c}{(6)}"     if n == 2
replace all6and  = " & \multicolumn{1}{c}{(7)}"     if n == 2
replace all7and  = " & \multicolumn{1}{c}{(8)}"     if n == 2
replace all8and  = " & \multicolumn{1}{c}{(9)}"     if n == 2
replace all9and  = " & \multicolumn{1}{c}{(10)}"    if n == 2

replace all1_0   = " "                    	      		 if n == 3
replace all0and  = " & \multicolumn{5}{c}{Singletons}"   if n == 3
replace all1and  = "   "           						 if n == 3
replace all2and  = "   "           						 if n == 3
replace all3and  = "   "           						 if n == 3
replace all4and  = "   "           						 if n == 3
replace all5and  = " & \multicolumn{5}{c}{Twins}"        if n == 3
replace all6and  = "   "           						 if n == 3
replace all7and  = "   "           						 if n == 3
replace all8and  = "   "           						 if n == 3
replace all9and  = "   "           						 if n == 3

replace all1_0   = " "                    	      		 if n == 4
replace all0and  = " & \multicolumn{2}{c}{Main Specification}"   if n == 4
replace all1and  = "   "           						 if n == 4
replace all2and  = " & \multicolumn{3}{c}{Alternative}"  if n == 4
replace all3and  = "   "           						 if n == 4
replace all4and  = "   "           						 if n == 4
replace all5and  = " & \multicolumn{2}{c}{Main Specification}"   if n == 4
replace all6and  = "   "           						 if n == 4
replace all7and  = " & \multicolumn{3}{c}{Alternative}"  if n == 4
replace all8and  = "   "           						 if n == 4
replace all9and  = "   "           						 if n == 4

replace all1_0   = " "                    	      		            if n == 5
replace all0and  = " & \multicolumn{1}{c}{ }"                       if n == 5
replace all1and  = " & \multicolumn{2}{c}{Bootstrap $ p $-value}"   if n == 5
replace all2and  = "   "           						            if n == 5
replace all3and  = " & \multicolumn{2}{c}{Analytic $ p $-value}"    if n == 5
replace all4and  = "   "           						            if n == 5
replace all5and  = " & \multicolumn{1}{c}{ }"                       if n == 5
replace all6and  = " & \multicolumn{2}{c}{Bootstrap $ p $-value}"   if n == 5
replace all7and  = "   "           						            if n == 5
replace all8and  = " & \multicolumn{2}{c}{Analytic $ p $-value}"    if n == 5
replace all9and  = "   "           						            if n == 5


replace all1_0   = " "                    	      		                if n == 6
replace all0and  = " & \multicolumn{1}{c}{ $\Delta$ }"                  if n == 6
replace all1and  = " & \multicolumn{1}{c}{  \tiny Child-Clustered }"    if n == 6
replace all2and  = " & \multicolumn{1}{c}{  \tiny State-Clustered }"    if n == 6
replace all3and  = " & \multicolumn{1}{c}{  \tiny Child-Clustered }"    if n == 6
replace all4and  = " & \multicolumn{1}{c}{  \tiny State-Clustered }"    if n == 6
replace all5and  = " & \multicolumn{1}{c}{ $\Delta$ }"                  if n == 6
replace all6and  = " & \multicolumn{1}{c}{  \tiny Child-Clustered }"    if n == 6
replace all7and  = " & \multicolumn{1}{c}{  \tiny State-Clustered }"    if n == 6
replace all8and  = " & \multicolumn{1}{c}{  \tiny Child-Clustered }"    if n == 6
replace all9and  = " & \multicolumn{1}{c}{  \tiny State-Clustered }"    if n == 6

replace all1_0   = "  \multicolumn{10}{l}{\textbf{\textit{Panel a}. Mean Difference}}"            				 if n == 7
replace all0and  = "   "           						      		                		   					 if n == 7
replace all1and  = "   "           						      		                		   					 if n == 7
replace all2and  = "   "           						      		                		   					 if n == 7
replace all3and  = "   "           						      		               			   					 if n == 7
replace all4and  = "   "           						      		               			   					 if n == 7
replace all5and  = "   "           						      		               			   					 if n == 7
replace all6and  = "   "           						      		                		   					 if n == 7
replace all7and  = "   "           						      		               			   					 if n == 7
replace all8and  = "   "           						      		               			   					 if n == 7
replace all9and  = "   "           						      		               			   					 if n == 7

replace all1_0   = "  \multicolumn{10}{l}{\textbf{\textit{Panel a}. OLS}}"            				             if n == 13
replace all0and  = "   "           						      		                		   					 if n == 13
replace all1and  = "   "           						      		                		   					 if n == 13
replace all2and  = "   "           						      		                		   					 if n == 13
replace all3and  = "   "           						      		               			   					 if n == 13
replace all4and  = "   "           						      		               			   					 if n == 13
replace all5and  = "   "           						      		               			   					 if n == 13
replace all6and  = "   "           						      		                		   					 if n == 13
replace all7and  = "   "           						      		               			   					 if n == 13
replace all8and  = "   "           						      		               			   					 if n == 13
replace all9and  = "   "           						      		               			   					 if n == 13

replace all1_0   = "  \multicolumn{10}{l}{\textbf{\textit{Panel a}. IPW}}"            				             if n == 19
replace all0and  = "   "           						      		                		   					 if n == 19
replace all1and  = "   "           						      		                		   					 if n == 19
replace all2and  = "   "           						      		                		   					 if n == 19
replace all3and  = "   "           						      		               			   					 if n == 19
replace all4and  = "   "           						      		               			   					 if n == 19
replace all5and  = "   "           						      		               			   					 if n == 19
replace all6and  = "   "           						      		                		   					 if n == 19
replace all7and  = "   "           						      		               			   					 if n == 19
replace all8and  = "   "           						      		               			   					 if n == 19
replace all9and  = "   "           						      		               			   					 if n == 19

replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Childcare, Ages 1 to 3}" 	           if n == 8  | n == 14 | n == 20
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Parenting, Ages 1 to 3}" 		 	   if n == 9  | n == 15 | n == 21
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} PPVT, Age 3}" 		 		       if n == 10 | n == 16 | n == 22
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Stanford-Binet, Age 3}" 		 	   if n == 11 | n == 17 | n == 23
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Average of Indices, Age 18}"         if n == 12 | n == 18 | n == 24


// order
global orderlist all1_0 all0and
foreach num of numlist 1(1)10 {
	global orderlist $orderlist all`num' all`num'and
}
order $orderlist

// other formating
replace all10and = " \\   "
replace all10and = " \\ \midrule " 					       if n == 2  | n == 6  | n == 12 | n == 18
replace all10and = " \\ \cmidrule(l{.15cm}r{.15cm}){2-6} \cmidrule(l{.15cm}r{.15cm}){7-11} " 															      if n == 3
replace all10and = " \\ \cmidrule(l{.15cm}r{.15cm}){2-3} \cmidrule(l{.15cm}r{.15cm}){4-6} \cmidrule(l{.15cm}r{.15cm}){7-8} \cmidrule(l{.15cm}r{.15cm}){9-11}"  if n == 4
replace all10and = " \\ \cmidrule(l{.15cm}r{.15cm}){3-4} \cmidrule(l{.15cm}r{.15cm}){5-6} \cmidrule(l{.15cm}r{.15cm}){8-9} \cmidrule(l{.15cm}r{.15cm}){10-11}" if n == 5
replace all10and = " \\ \bottomrule \end{tabular}"          if n == 24
replace all10and = " " 								       if n == 1

// put together in string
gen all = " "
foreach num of numlist 1(1)10 {
	tostring all`num'  , replace force format(%15.2fc)
	replace  all`num'  = "" if all`num'  == "."
}

global orderin all1_0 all0and
foreach num of numlist 1(1)10 {
	global orderin $orderin all`num' all`num'and 
}
global orderin $orderin all`num'
order $orderin n

foreach var of varlist all1_0-all10and {
	replace  all = all + `var'
	replace  all`num' = " " if all`num' == "."
}

// save in tex format
keep all
cd $output
outsheet using estimatorsrobust.tex, noquote nonames replace
